<?php
if(isset($_SESSION['QuyenHan']) && $_SESSION['QuyenHan'] == 2)
{
	$vanbanden = new vanbandenbs();
	
	if(isset($_POST['btnTim']))
	{
		
		$SovaKyHieu = $_POST['SovaKyHieu'];
		$NoiBanHanh = $_POST['NoiBanHanh'];
		$NgayBanHanh = str_replace('-',',',$_POST['NgayBanHanh']);
		$NgayHetHan = $_POST['TrichYeu'];
		
		
		$vanbanden->SovaKyHieu = $SovaKyHieu;
		$vanbanden->NgayBanHanh = $NgayBanHanh;
		$vanbanden->TrichYeu = $TrichYeu;
		$vanbanden->NoiBanHanh = $NoiBanHanh;
	
	}
	if(isset($_GET['ds'])&&$_GET['ds'] == 2)
	{
		$vanbanden->MaTrangThai = 4; 
		$mtt=2;
	}
	elseif(isset($_GET['ds'])&&$_GET['ds'] == 1)
	{
		 $vanbanden->MaTrangThai = 3; 
		 $mtt=1;
	}
	else
		 $vanbanden->MaTrangThai = 2; 
	$vanbanden->MaPhongXuLy = $_SESSION['MaPhongBan'];
	$dsvb = $vanbanden->GetTrangThaiVanBan();
	$tongsodong =  count($dsvb);
	
	if(!isset($_GET['start']))
		$start = 0;
	else
		$start = $_GET['start'];
	
	$limit = 15;
	$next = $start + $limit;
	$back = $start - $limit;
	$danhsach = $vanbanden->pageTrangThaiVanBan($start, $limit);
	
	$nguoixuly = new nguoidungbs();
	$nguoixuly->MaPhongBan = $_SESSION['MaPhongBan'];
	$dsnguoixuly = $nguoixuly->GetList();
	
	
?>

<div id="NoiDung" style="position:relative" class="form">
<?php if(isset($_GET['ds'])&&$_GET['ds'] == 2){ ?>
  <h3  class="text-primary">VĂN BẢN NHÂN VIÊN ĐÃ XỬ LÝ</h3>
<?php }elseif(isset($_GET['ds'])&&$_GET['ds'] == 1){ ?>
 <h3  class="text-primary">VĂN BẢN NHÂN VIÊN CHƯA XỬ LÝ</h3>
<?php }else {?>
 <h3  class="text-primary">VĂN BẢN ĐẾN</h3>
<?php }?>

  <div style="position:absolute; left:840px; top:10px;"><a href="#"  style="margin-left:10px"><span id="clickSearch" class="glyphicon glyphicon-search" style="font-size:18px"></span></a></div>
  <div style="margin-top:20px; display:none" id="search">
    <form  action="vanbanden-phongban-xuly" method="post" role="form">
      <table class="table borderless">
        <tbody>
          <tr>
          <td><div class="form-group form-custom">
              <label  for="sovakyhieu">Số và ký hiệu:</label>
              <input type="text" class="form-control" placeholder="Số và ký hiệu" name="SovaKyHieu" class="required" />
            </div></td>
          <td><div class="form-group form-custom">
              <label  for="noibanhanh">Nơi ban hành:</label>
              <input type="text" class="form-control" placeholder="Nơi ban hành" name="NoiBanHanh" class="required" />
            </div></td>
        </tr>
        <tr>
          <td><div class=" form-group form-custom control-group">
              <label  for="trichyeu">Trích yếu</label>
              <input type="text" class="form-control" placeholder="Trích yếu" name="TrichYeu" class="required" />
            </div></td>
          <td><div class=" form-group form-custom control-group">
              <label class="control-label">Ngày ban hành</label>
              <div class="controls input-append date form_date" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                <input size="16" type="text" value="" readonly="readonly" name="NgayBanHanh" class="form-control">
                <span class="add-on"><i class="icon-remove"></i></span> <span class="add-on"><i class="icon-th"></i></span> </div>
            </div></td>
        </tr>
        <tr>
        <td colspan="2">
        <input type="submit" value="Tìm" class="btn btn-primary btn-primary pull-right" name="btnTim" />
         </td>
        </tr>
       </tbody>
        
      </table>
    </form>
  </div>
  <script>
 $("#clickSearch").click(function()
	{
		$("#search").toggle("fast");
})

 </script> 
 
 <div class="btnXemLaiVanBan">
 		<?php
        	if(isset($_GET['ds'])&&$_GET['ds'] == 2)
			{
			?> <a href="vanbanden-phongban-xuly" >
			 <label class= 'label label-danger'>Trở lại</label>
			</a>
        
   		<?php
			}
        	if(isset($_GET['ds'])&& $_GET['ds'] == 1)
			{
		?>
        <a href="vanbanden-phongban-xuly_0" >
         <label class= 'label label-danger'>Trở lại</label>
        </a>
        
        <a href="vanbanden-phongban-xuly_2" >
         <label class= 'label label-danger'>Nhân viên đã xử lý</label>
        </a>
        
        <?php
			}else
			{?>
          <a href="vanbanden-phongban-xuly_1" >
         <label class= 'label label-danger'>Nhân viên chưa xử lý</label>
        </a> 
        <?php }?>
        
</div>
 
  <!--Bang van ban den-->
  
  <div style="margin-top:20px;">
<form action="vanbanden-phongban-xuly" method="post">
    <table class="table table-hover table-bordered table-striped">
      <thead>
        <tr>
          <th style="text-align:center"> STT </th>
         
          <th  style="text-align:center"> Số và ký hiệu </th>
         
          <th style="text-align:center"> Ngày ban hành </th>
          <th  style="text-align:center"> Trích yếu </th>
        
            <th style="text-align:center"> Nơi ban hành</th>
             <?php if(!(isset($_GET['ds'])&&$_GET['ds'] == 1 ))
		  	{
				if(!$_GET['ds'] == 2)
				{
		  		?>
             <th style="text-align:center"> Người xử lý</th>
             <?php }?>
          <th style="text-align:center"> <span class="glyphicon glyphicon-ok"></span> </th>
           <?php }?>
          
        </tr>
      </thead>
      <tbody>
        <?php
		
	if(!is_null($danhsach[0]['MaVanBanDen']))
	{
		
				for($i = 0; $i < count($danhsach); $i++)
				{
					$stt=$i+1;
					echo "<tr>";
						
						echo "<td>".$stt."</td>";
						
						if(isset($_POST['SovaKyHieu']))
							echo "<td> <a href='vanbanden-chitiet_{$danhsach[$i]['MaVanBanDen']}'>".highlight($danhsach[$i]['SovaKyHieu'], $SovaKyHieu)."</a></td>";
						else
							echo "<td> <a href='vanbanden-chitiet_{$danhsach[$i]['MaVanBanDen']}'>{$danhsach[$i]['SovaKyHieu']} </a></td>";
						
					
						
					
						if(isset($_POST['NgayBanHanh']))
							echo "<td>".highlight(InNgay($danhsach[$i]['NgayBanHanh']), str_replace(',','/',$NgayBanHanh) )."</td>";
						else
							echo "<td>".InNgay($danhsach[$i]['NgayBanHanh'])."</td>";
						
						if(isset($_POST['TrichYeu']))
							echo "<td>".highlight($danhsach[$i]['TrichYeu'],$TrichYeu)."</td>";
						else
							echo "<td>{$danhsach[$i]['TrichYeu']}</td>";
						
						if(isset($_POST['NoiBanHanh']))
							echo "<td>".highlight($danhsach[$i]['NoiBanHanh'],$NoiBanHanh)."</td>";
						else
							echo "<td>{$danhsach[$i]['NoiBanHanh']}</td>";
					
						if(!(isset($_GET['ds'])&&$_GET['ds'] == 1))
						{
							if(!$_GET['ds'] == 2)
							{
								echo "<td>
									<select name='MaNguoiXuLy[]'>";
									for($j=0;$j<count($dsnguoixuly);$j++)
									{
										if($dsnguoixuly[$j]['QuyenHan'] == 3)
										echo "<option value=".$dsnguoixuly[$j]['MaNguoiDung'].">{$dsnguoixuly[$j]['TenNguoiDung']}</option>";
									}
								echo"	</select>
								</td>";
							}
						
							echo "<td><input type='checkbox' name='checkDuyet[]' value=".$danhsach[$i]['MaVanBanDen']." /></td>";
							
						}
				echo "</tr>";
				}
			}
			?>
             <?php if(!(isset($_GET['ds'])&&$_GET['ds'] == 1 ))
		  {
		  ?>
            <tr>
            	<td colspan="9">
                	<input value="Xác nhận"  class="btn btn-primary btn-primary pull-right" name="btnDuyet" type="submit" />
                </td>
            </tr>
            <?php }?>
      </tbody>
    </table>
<form>
  </div>
  <?php
		if($tongsodong > $limit)
		{
			echo "<div id='PhanTrang' align = 'center'>";
				echo "<ul class='pagination pagination-sm'>";
					if(isset($_GET['ds'])&& $_GET['ds'] == 2 ||  $_GET['ds'] == 1)
					{
						if($start > 0)
							echo "<li><a href='vanbanden-phongban-xuly-page-loai_$mtt-$back'>&laquo;</a></li>";
						$j = 1;
						for($i = 0; $i < $tongsodong; $i += $limit)
						{		
								echo "<li><a href='vanbanden-phongban-xuly-page-loai_$mtt-$i'>" . $j . "</a></li>";
							$j++;
						}
						if($start + $limit < $tongsodong)
							echo "<li><a href='vanbanden-phongban-xuly-page-loai_$mtt-$next'>&raquo;</a></li>";
					}else
					{
												if($start > 0)
							echo "<li><a href='vanbanden-phongban-xuly-page_$back'>&laquo;</a></li>";
						$j = 1;
						for($i = 0; $i < $tongsodong; $i += $limit)
						{		
								echo "<li><a href='vanbanden-phongban-xuly-page_$i'>" . $j . "</a></li>";
							$j++;
						}
						if($start + $limit < $tongsodong)
							echo "<li><a href='vanbanden-phongban-xuly-page_$next'>&raquo;</a></li>";

					}
				echo "</ul>";
				
			echo "</div>";
			echo "<br/>";
		}
?>
</div>
<script type="text/javascript">
   
	$('.form_date').datetimepicker({
      	format: 'dd-mm-yyyy',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0
    });
	
</script>


<?php
	if(isset($_POST['btnDuyet']))
	{
		$MaVanBanDen = $_POST['checkDuyet'];
		$MaNguoiXuLy = $_POST['MaNguoiXuLy'];
		$tem = new vanbandenbs();
		$tem->MaVanBanDen = $MaVanBanDen;
		$ds=$tem->GetList();
		$vb = new vanbandenbs();
		if($ds->MaTrangThai == 4)
		$vb->MaTrangThai = 7;
		else
		$vb->MaTrangThai = 3;
		
		for($i=0; $i < count($MaVanBanDen); $i++)
		{
			$vb->MaVanBanDen = $MaVanBanDen[$i];
			$vb->MaNguoiXuLy = $MaNguoiXuLy[$i];
			$vb->ChangeTrangThai();
		}
		if($ds->MaTrangThai == 4)
		header("Location: vanbanden-phongban-xuly_2");
		else
		header("Location: vanbanden-phongban-xuly_1");
	}
}else
{
	ThongBaoLoi("Vui lòng đăng nhập !");
}
?>